/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import javax.microedition.lcdui.*;
import java.util.Vector;
import ral.Real;
import conexion.Conexion;
/**
 * @author REX
 */
public class MenuCines extends Canvas implements CommandListener {
    Vector listaCines = new Vector(5,2);
    private int width,height;
    private int anchor = Graphics.TOP | Graphics.LEFT;
    private MenuPrincipal mp;
    private int visibles = 3;
    private int barTitulo = 30;
    private int altoVisible;
    private int scrollY;
    private FuncionalidadMenuCines fmc;
    public MenuCines(MenuPrincipal mp) {
        try {
            width = getWidth();
            height = getHeight();
	    setCommandListener(this);
	    addCommand(new Command("Salir", Command.EXIT, 1));
            obtenListaDeCines();
            this.mp = mp;
            altoVisible =  height - barTitulo ;
            scrollY =  barTitulo;
            fmc = new FuncionalidadMenuCines(0,listaCines.size() - 1,0,scrollY,visibles);
            
        } catch(Exception e) {
            e.printStackTrace();
        }
    } 
    
    /**
     * paint
     */
    public void paint(Graphics g) {
        limpiaPantalla(g);
        Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
        g.setFont(f);
        g.setColor(0x32639A);
        /*Titulo*/
        String titulo = "Lista De Cines";
        int anchoTitulo = f.stringWidth(titulo);
        int altoTitulo = f.getHeight();
        int posXt = (width - anchoTitulo)/2;
        int posYt = (int)(height/9) - altoTitulo;
        g.drawString(titulo,posXt ,posYt , anchor);
        g.setColor(0x4486C7);
        g.setStrokeStyle(Graphics.SOLID);
        g.drawLine(0, barTitulo - 1, width, barTitulo -1);
        g.drawLine(0, barTitulo, width, barTitulo);

        /*
         Tamaño de los cuadros de las opciones
         */
        Font f2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f2);
      
        int nW =(int)(width);
        int nH = ((int)(altoVisible)   )/visibles;
        int deltax = (int)(width*0.00);
        int deltay = barTitulo;
        int y;
        /*Scroll bar*/
      /*  int sbbW = (int)(width*0.05);
        int sbbH = (int)altoVisible;
        g.setColor(0xCCCCFF);
        g.fillRect(nW, barTitulo, sbbW, sbbH);
        
        int sbW = (int)(width*0.05);
        int sbH = (int)(altoVisible/(listaCines.size()));
        fmc.setScrollHeight(sbH);*/
        /************/
        int k = 0;
        for(int i=fmc.getInicial();i<=fmc.getFinal();i++){
            y = deltay +  (nH)*k;
            int delayf = deltax;
            if(fmc.getActual() == i)
                dibujaSeleccionado( g, deltax, delayf, y, nW, nH,   i);
            else
                dibujaNoSeleccionado( g, deltax, delayf, y, nW, nH,  i);
        
            k++;

        }
    }
    public void dibujaScroll(Graphics g, int x, int y, int w,int h){
        g.setColor(0x9900CC);
        g.fillRect(x, y, w, h);
    }
    public void dibujaSeleccionado(Graphics g,int deltax,int delayf,int y,int nW,int nH,int  i){
        Cine c = (Cine)listaCines.elementAt(i);
        Font f2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f2);
        g.setColor(0xE0FFFF);
        g.fillRect( deltax, y , nW, nH );
        g.setColor(0x5D781D);
        g.drawString(c.getNombre(), delayf, y , anchor);
        g.setColor(0xA8A8A8 );
        Font f3 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        g.setFont(f3);
        g.drawString("Distancia " + c.calculaDistancia().toString(), delayf, y + nH/2, anchor);
        g.setColor(0x89B056);
        g.drawRect( deltax, y, nW, nH);
    }
    public void dibujaNoSeleccionado(Graphics g,int deltax,int delayf,int y,int nW,int nH,int  i){
        Cine c = (Cine)listaCines.elementAt(i);
        Font f2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f2);
        g.setColor(0xFFFFFF);
        g.fillRect( deltax, y , nW, nH );
        g.setColor(0x5D781D);
        g.drawString(c.getNombre(), delayf, y , anchor);
        g.setColor(0xA8A8A8 );
        Font f3 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        g.setFont(f3);
        g.drawString("Distancia " + c.calculaDistancia().toString(), delayf, y + nH/2, anchor);
        g.setColor(0x89B056);
        g.drawRect( deltax, y, nW, nH);
    }
    public void limpiaPantalla(Graphics g){
        g.setColor( 255, 255, 255 );
        g.fillRect( 0, 0, width, height );
    }
    public void obtenListaDeCines(){
        /*Aqui deberia hacerse una peticion al servidor
         y cargarse la lista de cines
         */
      // String respuesta =  Conexion.peticionGETconParametro("http://www.ngwise.com/cm/cartelera.php","idcine",1001);
       listaCines.addElement(new Cine(1001,"Cinemex Mundo E",new Real("19.304"),new Real("-99.22")));
       listaCines.addElement(new Cine(1002,"Cinemex Mundo E",new Real("19.525241"),new Real("19.525241")));
       listaCines.addElement(new Cine(1003,"Cinemark Parque Tezontle",new Real("19.383461"),new Real("-99.082839")));
       listaCines.addElement(new Cine(1001,"Cinepolis Perisur",new Real("19.304"),new Real("-99.22")));
       listaCines.addElement(new Cine(1002,"Cinemex Mundo E",new Real("19.525241"),new Real("19.525241")));
       listaCines.addElement(new Cine(1003,"Cinemark Parque Tezontle",new Real("19.383461"),new Real("-99.082839")));
       /*
          for(int i=0; i<c.size(); i++){
            Cine c2 = (Cine)c.elementAt(i);
            System.out.print(c2.getNombre()+"\t");
        }*/

    }
    /**
     * Called when a key is pressed.
     */
    protected  void keyPressed(int keyCode) {
         String str = getKeyName(keyCode);
        if (str.equals("Up") || str.equals("UP")) {
            fmc.subir();
            repaint();
        }if (str.equals("Down") || str.equals("DOWN")) {
            fmc.bajar();
            repaint();
        }if(str.equals("Right") || str.equals("RIGHT") || str.equals("Fire") || str.equals("FIRE")){


        }if(str.equals("Left") || str.equals("LEFT")){
            mp.getMidlet().pantalla.setCurrent(mp);
        }
    }

    public void commandAction(Command c, Displayable displayable) {
         if (c.getLabel().equals("Salir")) {
            mp.getMidlet().exit();
         }
    }

}
